<script lang="ts" setup>
import RightTitle from './RightTitle.vue'
import { barOption } from '../../utils/option'
import { ref } from 'vue'
import { staffTurnover } from '../../request/air'
import charts from '../../components/charts.vue'
import * as echarts from 'echarts'
import useResize from '../../hook/Resize'
useResize()
const key = ref(0)
// 定义图表渐变颜色数组
let colorArr = [
    [
        {
            offset: 0,
            color: '#2339ff'
        },
        {
            offset: 0.5,
            color: '#731cff'
        },
        {
            offset: 1,
            color: '#ba02ff'
        }
    ],
    [
        {
            offset: 0,
            color: '#3ee293'
        },
        {
            offset: 0.5,
            color: '#21bf52'
        },
        {
            offset: 1,
            color: '#049c11'
        }
    ]
]
// 发请求获取数据
staffTurnover().then(resp => {
    // console.log('aaa', resp);
    let data = resp.data.data.result[0]
    // console.log('bbb', data);
    let arr: any[] = []
    data.dimensions.forEach((v: string | number) => {
        arr.push(data.source.map((item: { [x: string]: any }) => item[v]))
    })
    barOption.xAxis = {
        type: 'category',
        data: arr.shift()
    }
    barOption.series = arr.map((v, index) => {
        return {
            type: 'bar',
            data: v,
            barWidth: 10,
            name: data.dimensions[index + 1],
            itemStyle: {
                // 圆角
                borderRadius: [20, 20, 20, 20],
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, colorArr[index])
            }
        }
    })

    // console.log('ccc', barOption);
    key.value = +new Date
})

</script>

<template>
    <div class="right-cotainer">
        <!-- 上面的标题 -->
        <Right-Title title="校园人员流动情况"></Right-Title>
        <!-- 下面部分 -->
        <div class="echarts">
            <!-- 上面头像部分 -->
            <div class="avatar">
                <div>
                    <img src="../../assets/昨日访客.png" alt="">
                    <div class="wrap">
                        <span>1</span>
                        <p>昨日外来访客</p>
                    </div>
                </div>
                <div>
                    <img src="../../assets/今日访客.png" alt="">
                    <div class="wrap">
                        <span>1</span>
                        <p>今日外来访客</p>
                    </div>
                </div>
            </div>
            <!-- 图表 -->
            <div class="rb-charts">
                <charts ref="mycharts" :option="barOption" :key="key"></charts>
            </div>
        </div>
    </div>
</template>

<style scoped lang="scss">
.right-cotainer {
    width: 100%;
    height: 45%;
    position: absolute;
    right: 0;
    bottom: 0;
    padding: 0 50px 0 70px;
    box-sizing: border-box;

    .echarts {
        width: 100%;
        height: 70%;
        background-color: rgba(120, 120, 120, .1);
        color: #ccc;

        .avatar {
            width: 100%;
            height: 50px;
            display: flex;
            justify-content: space-around;
            align-items: center;

            >div {
                display: flex;
                justify-content: space-between;
                align-items: center;

                .wrap {
                    text-align: center;
                    font-size: 12px;
                    margin-left: 20px;

                    p {
                        padding: 0;
                        margin: 0;
                    }
                }

                img {
                    width: 20px;
                    height: 20px;
                }
            }
        }

        .rb-charts {
            width: 100%;
            height: 100%;
        }
    }
}
</style>